//
//  ImageSelectV.swift
//  Employee
//
//  Created by swordjoy on 2017/3/27.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Photos

class ImageSelectV: UIView {
  
    var assets: [PHAsset] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
  /// 传入闭包,增加图片
  var addPhotoClosure: VoidClosure?
  
  /// 传入闭包,响应删除选中图片
  var deleteClosure: ( (Int) -> () )?
  
  var cellDidClickAction: ( (Int) -> () )?
  
  fileprivate var addLabel: UILabel!
  fileprivate var collectionView: UICollectionView!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    
    create()
  }

  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  deinit {
    SJApp.log("ImageSelectV deinit")
  }
  
}


// MARK: - fileprivate

extension ImageSelectV {
  
  fileprivate func create() {
    let flow = UICollectionViewFlowLayout()
    let width = (UIScreen.width - 80) / 4
    flow.itemSize = CGSize(width: width, height: width)
    flow.minimumInteritemSpacing = 10
    
    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
    collectionView.backgroundColor = UIColor.clear
    collectionView.contentInset = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
    addSubview(collectionView)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(ImageSelectCell.self, forCellWithReuseIdentifier: ImageSelectCell.identifier)
    collectionView.register(ChooseImageCell.self, forCellWithReuseIdentifier: ChooseImageCell.identifier)
    collectionView.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(5)
      make.height.equalToSuperview()
    }
  }
}

extension ImageSelectV: UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return assets.count < 4 ? assets.count + 1 : 4
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    // 数量小于4,增加cell才会出现
    if indexPath.row == assets.count {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChooseImageCell.identifier, for: indexPath) as! ChooseImageCell
      
      cell.addPhotoClosure = { [weak self] in
        guard let closure = self?.addPhotoClosure else { return }
        closure()
      }
      
      return cell
    }
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageSelectCell.identifier, for: indexPath) as! ImageSelectCell
    
    cell.deleteClosure = { [weak self] (index) in
      guard let closure = self?.deleteClosure else { return }
      self?.assets.remove(at: index)
      collectionView.reloadData()
      closure(index)
    }
    cell.asset = assets[indexPath.row]
    cell.index = indexPath.row
    return cell
  }
}

extension ImageSelectV: UICollectionViewDelegate {
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    cellDidClickAction?(indexPath.row)
  }
  
}
